package com.lgx.mall.web.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lgx.common.entity.CityDictonary;
import com.lgx.common.entity.Dictionary;
import com.lgx.common.exception.DataException;
import com.lgx.common.service.ICityDictonaryService;
import com.lgx.common.web.Constants;
import com.lgx.common.web.Pagination;
import com.lgx.common.web.util.DateUtils;
import com.lgx.common.web.util.DictUtils;
import com.lgx.common.web.util.EscapeUtils;
import com.lgx.mall.basic.entity.Address;
import com.lgx.mall.basic.service.IAddressService;
import com.lgx.mall.business.entity.ConsumerInfo;
import com.lgx.mall.business.entity.Order;
import com.lgx.mall.business.entity.OrderDetail;
import com.lgx.mall.business.entity.ReturnRequest;
import com.lgx.mall.business.entity.ScoreGiftRecords;
import com.lgx.mall.business.service.IConsumerInfoService;
import com.lgx.mall.business.service.IOrderDetailService;
import com.lgx.mall.business.service.IOrderService;
import com.lgx.mall.business.service.IReturnRequestService;
import com.lgx.mall.business.service.IScoreGiftRecordService;
import com.lgx.mall.core.entity.ScoreCard;
import com.lgx.mall.core.service.IProductService;
import com.lgx.mall.core.service.IScoreCardService;
import com.lgx.right.entity.Card;
import com.lgx.right.entity.User;
import com.lgx.right.service.IUserService;

/**
 * 
 * @author acer
 *
 */
@Controller
@RequestMapping("/member")
public class MemberController {
	
	@Autowired
	private IUserService userService;
	
	@Autowired
	private IAddressService addressService;     //收货地址
	
	@Autowired
	private ICityDictonaryService cityDictonaryService;
	
	@Autowired
	private IScoreCardService scoreCardService;    //积分卡
	
	@Autowired
	private IScoreGiftRecordService scoreGiftRecordService;   //积分赠送 
	
	@Autowired
	private IConsumerInfoService consumerInfoService;      //消费记录
	
	@Autowired
	private IOrderService orderService;               //订单
	
	@Autowired
	private IOrderDetailService orderDetailService;   //订单明细
	
	@Autowired
	private IProductService productService;           //商品
	
	@Autowired
	private IReturnRequestService returnRequestService;   //退货记录

	
	public IReturnRequestService getReturnRequestService() {
		return returnRequestService;
	}

	public IProductService getProductService() {
		return productService;
	}

	public IOrderDetailService getOrderDetailService() {
		return orderDetailService;
	}

	public IOrderService getOrderService() {
		return orderService;
	}

	public IConsumerInfoService getConsumerInfoService() {
		return consumerInfoService;
	}

	public IScoreGiftRecordService getScoreGiftRecordService() {
		return scoreGiftRecordService;
	}

	public IScoreCardService getScoreCardService() {
		return scoreCardService;
	}

	public ICityDictonaryService getCityDictonaryService() {
		return cityDictonaryService;
	}

	public IAddressService getAddressService() {
		return addressService;
	}


	public IUserService getUserService() {
		return userService;
	}


	
	@RequestMapping("/index.htm")
	public String index(HttpServletRequest request,HttpServletResponse response){
		String path = request.getContextPath();
		String basePath = request.getScheme() + "://"
				+ request.getServerName() + ":" + request.getServerPort()
				+ path + "/";
		request.setAttribute("basePath", basePath);
		
		String url = request.getParameter("url");   //这个url携带的是购物车的路径
		
		if(!StringUtils.isEmpty(url))request.setAttribute("url", url);
		
		return "member/index";
	}
	
	
	/**
	 * updateLoginUser.do
	 * 修改登录会员跳转
	 */
	@RequestMapping("/loginMumber.htm")
	public String loginMumber(HttpServletRequest request,HttpServletResponse response){
		
		List<Dictionary> sexTypes = DictUtils.getDictListByKind(DictUtils.SEX_TYPE);
		request.getSession().setAttribute("sexTypes", sexTypes);
		
		//得到当前登录人的一些必要信息
		User onlineUser = (User) request.getSession().getAttribute(Constants.LOGIN_USER);
		request.setAttribute("onlineUser", onlineUser);    //存入当前登录人对象
		
		return "member/modify/member-init";
	}
	
	
	//修改实现
	@RequestMapping("/update.htm")
	@ResponseBody
	public String update(HttpServletRequest request,HttpServletResponse response){
		String userId = request.getParameter("userId");
		
		String truename = request.getParameter("truename");
		if(!StringUtils.isEmpty(truename))truename = EscapeUtils.unescape(truename);
		
		String nickname = request.getParameter("nickname");
		if(!StringUtils.isEmpty(nickname))nickname = EscapeUtils.unescape(nickname);
		
		Integer sex = Integer.parseInt(request.getParameter("sex"));  //1 --  先生   0 --  小姐
		
		String email = request.getParameter("email");
		if(!StringUtils.isEmpty(email))email = EscapeUtils.unescape(email);
		
		User user = this.userService.findUserById(Long.parseLong(userId));   
		//将数据存入user实体
		user.setTruename(truename);     //用户真实姓名
		user.setNickname(nickname);     //用户昵称
		user.setSex(sex);               //用户性别 
		user.setEmail(email);           //用户邮箱
		
		//调用后台
		 try {
			user = userService.updateUser(user);
		} catch (DataException e) {
			e.printStackTrace();
			return "0";
		}
		 
		request.getSession().setAttribute(Constants.LOGIN_USER, user);
		return "1";
	}
		
		
		/**
		 * 处理修改当前登录人密码的操作密码判断
		 * @param request
		 * @param response
		 * @return
		 */
		@RequestMapping("/modifyPassword.htm")
		@ResponseBody
		public String modifyPassword(HttpServletRequest request,HttpServletResponse response){
			User onlineUser = (User) request.getSession().getAttribute(Constants.LOGIN_USER);    //得到当前登录人信息,以保证不会删除到别的用户 
			
			String opwd = request.getParameter("opwd");
			String npwd = request.getParameter("npwd");
			
			//原密码验证
			
			try {
				return this.userService.modifyPassword(onlineUser.getId(), npwd, opwd);
			} catch (DataException e) {
				e.printStackTrace();
				return "2";
			}
		}
		
		
		/**
		 * 收货地址管理跳转
		 */
		@RequestMapping("/address.htm")
		public String memberAddressURL(HttpServletRequest request,HttpServletResponse response){
			
			//得到当前登录用户
			User onlineUser = (User) request.getSession().getAttribute(Constants.LOGIN_USER);    //得到当前登录人信息,以保证不会删除到别的用户 
			
			//根据用户查询
			List<Address> list = this.addressService.findAddressList(onlineUser.getId());
			
			request.setAttribute("list", list);
			
			String addressId = request.getParameter("addressId");
			String otype = request.getParameter("otype");
			
			if(!StringUtils.isEmpty(addressId)){//如果有id，则表示之前已经存在有数据，这次是根据id查询数据并加以显示
				Address address = this.addressService.findAddressById(Long.parseLong(addressId));
				request.setAttribute("address", address);
			}
			
			request.setAttribute("otype", otype);
			
			//加载省级市联省,初次加载省级
			List<CityDictonary> provinces = cityDictonaryService.findCityDictonaryList("province", "");
			request.setAttribute("provinces", provinces);
			
			return "member/address/addressMgr";
		}
		
		
		/**
		 * 省级市联，根据省code查询其下的市
		 */
		@RequestMapping("/initCityCounty.htm")
		@ResponseBody
		public String initProvinceCityCounty(HttpServletRequest request,HttpServletResponse response){
			String type = request.getParameter("type");
			String code = request.getParameter("code");
			
			List<CityDictonary> cds = cityDictonaryService.findCityDictonaryList(type, code);
			String ds = "";
			for(CityDictonary cd : cds){
				if(!StringUtils.isEmpty(ds))ds+="|";
				ds +=cd.getCode()+","+cd.getName();
			}
			
			return ds;
		}
		
		/**
		 * 添加
		 * @param request
		 * @param response
		 * @return
		 */
		@RequestMapping("/save.htm")
		@ResponseBody
		public String save(HttpServletRequest request,HttpServletResponse response){
			//接受系列参数
			String name = request.getParameter("name");
			if(!StringUtils.isEmpty(name))name = EscapeUtils.unescape(name);
			
			String tel = request.getParameter("tel");
			
			String province = request.getParameter("province");
			if(!StringUtils.isEmpty(province))province = EscapeUtils.unescape(province);
			
			String city = request.getParameter("city");
			if(!StringUtils.isEmpty(city))city = EscapeUtils.unescape(city);
			
			String region = request.getParameter("region");
			if(!StringUtils.isEmpty(region))region = EscapeUtils.unescape(region);
			
			String address = request.getParameter("address");
			if(!StringUtils.isEmpty(address))address = EscapeUtils.unescape(address);
			
			String zipcode = request.getParameter("zipcode");
			
			String isdefault = request.getParameter("isdefault");
			
			//得到当前登录用户
			User onlineUser = (User) request.getSession().getAttribute(Constants.LOGIN_USER);    //得到当前登录人信息,以保证不会删除到别的用户 
			if(Integer.parseInt(isdefault)==1){       //表示这条信息被选中为默认，清除原有的默认
				List<Address> list = addressService.findAddressList(onlineUser.getId(),1);         //去数据库查找这个用户的现有isdefault=1的收货地址欣信息，返回list，统计list数据条数
				if(list.size() > 0){
					Address asd = list.get(0);
					asd.setIsdefault(0);
					try {
						addressService.updateAddress(asd);
					} catch (DataException e) {
						e.printStackTrace();
					}
				}
			}
			
			//执行添加
			Address ads = new Address();
			ads.setName(name);
			ads.setTel(tel);
			ads.setProvince(province);
			ads.setCity(city);
			ads.setRegion(region);
			ads.setZipcode(zipcode);
			ads.setAddress(address);
			ads.setIsdefault(Integer.parseInt(isdefault));
			ads.setUser(onlineUser);  
			
			try {
				addressService.createAddress(ads);
				return "1";
			} catch (DataException e) {
				return "0";
			}
		}
		
		@RequestMapping("/updateAddress.htm")
		@ResponseBody
		public String updateAddress(HttpServletRequest request,HttpServletResponse response){
			//接受系列参数
			String id = request.getParameter("id");
			String name = request.getParameter("name");
			if(!StringUtils.isEmpty(name))name = EscapeUtils.unescape(name);
			
			String tel = request.getParameter("tel");
			
			String province = request.getParameter("province");
			if(!StringUtils.isEmpty(province))province = EscapeUtils.unescape(province);
			
			String city = request.getParameter("city");
			if(!StringUtils.isEmpty(city))city = EscapeUtils.unescape(city);
			
			String region = request.getParameter("region");
			if(!StringUtils.isEmpty(region))region = EscapeUtils.unescape(region);
			
			String address = request.getParameter("address");
			if(!StringUtils.isEmpty(address))address = EscapeUtils.unescape(address);
			
			String zipcode = request.getParameter("zipcode");
			
			String isdefault = request.getParameter("isdefault");
			
			//得到当前登录用户
			User onlineUser = (User) request.getSession().getAttribute(Constants.LOGIN_USER);    //得到当前登录人信息,以保证不会删除到别的用户 
			if(Integer.parseInt(isdefault)==1){       //表示这条信息被选中为默认，清除原有的默认
				List<Address> list = addressService.findAddressList(onlineUser.getId(),1);         //去数据库查找这个用户的现有isdefault=1的收货地址欣信息，返回list，统计list数据条数
				if(list.size() > 0){
					Address asd = list.get(0);
					asd.setIsdefault(0);
					try {
						addressService.updateAddress(asd);
					} catch (DataException e) {
						e.printStackTrace();
					}
				}
			}
			
			//执行添加
			Address ads = this.addressService.findAddressById(Long.parseLong(id));
			ads.setName(name);
			ads.setTel(tel);
			ads.setProvince(province);
			ads.setCity(city);
			ads.setRegion(region);
			ads.setZipcode(zipcode);
			ads.setAddress(address);
			ads.setIsdefault(Integer.parseInt(isdefault));
			
			try {
				addressService.updateAddress(ads);
				return "1";
			} catch (DataException e) {
				return "0";
			}
		}
		
		@RequestMapping("/delete.htm")
		@ResponseBody
		public String delete(HttpServletRequest request,HttpServletResponse response){
			String id = request.getParameter("id");
			try {
				addressService.deleteAddressById(Long.parseLong(id));
			} catch (NumberFormatException e) {
				return "0";
			} catch (DataException e) {
				return "0";
			}
			return "1";
		}
		
		
		/**
		 * bindingMobile.htm
		 * 绑定手机跳转
		 */
		@RequestMapping("/bindingMobile.htm")
		public String bindingMobile(HttpServletRequest request,HttpServletResponse response){
			//得到当前登录人的一些必要信息
			User onlineUser = (User) request.getSession().getAttribute(Constants.LOGIN_USER);
			request.setAttribute("onlineUser", onlineUser);    //存入当前登录人对象
			return "member/modify/updateMobile";
		}
		
		
		/**
		 * 处理修改绑定手机号码
		 * @param request
		 * @param response
		 * @return
		 */
		@RequestMapping("/jBoxUpdateUserMobile.htm")
		@ResponseBody
		public String jBoxUpdateUserMobile(HttpServletRequest request,HttpServletResponse response){
			String nMobile = request.getParameter("nMobile");                            //接受客户端传递的新电话号码
			Long userId = Long.parseLong(request.getParameter("userId"));              //接受客户端传递过来的id
			try {
				@SuppressWarnings("unused")
				User user = userService.updateUserMobile(userId, nMobile);
				return "1";    //成功返回1
			} catch (DataException e) {
				e.printStackTrace();
				return "0";
			}
		}
		
		
		/**
		 * 积分充值跳转
		 */
		@RequestMapping("/score.htm")
		public String score(HttpServletRequest request,HttpServletResponse response){
			//得到当前登录人的一些必要信息
			User onlineUser = (User) request.getSession().getAttribute(Constants.LOGIN_USER);
			request.setAttribute("onlineUser", onlineUser);    //存入当前登录人对象
			return "member/recharge/score";
		}
		
		
		/**
		 * 预付积分（积分卡充值）
		 */
		@RequestMapping("/recharge.htm")
		@ResponseBody
		public String recharge(HttpServletRequest request,HttpServletResponse response){
			String userId = request.getParameter("userId");   //接受充值人id
			
			String card = request.getParameter("card");       //接受充值卡号
			if(!StringUtils.isEmpty(card))card = EscapeUtils.unescape(card);
			
			String password = request.getParameter("password"); //密码
			if(!StringUtils.isEmpty(password))password = EscapeUtils.unescape(password);
			
			String time = DateUtils.dateToChar(new Date(), DateUtils.DATE_FORMAT_D);  //充值时间，如果这个卡的有效时间小于充值时间，则表示这个卡过期了

			
			//检查这个积分卡是否可以使用
			List<ScoreCard> scs = scoreCardService.checkedScoreCard(card,password,time);
			
			ScoreCard sCard  = null;
			
			if(scs.size() > 0){  //积分卡可用
				sCard  = scs.get(0);
			}
			
			if(sCard!= null){  //表示这条语句是符合充值条件的
				//向这个userId对应的用户的card.score加上查出的积分卡上的score值,如果添加成，则把这个积分卡的score值变为0，状态变成1
				User user = userService.findUserById(Long.parseLong(userId));
				if(user != null){
					try {
						Card c = user.getCard();
						c.setScore(c.getScore()+sCard.getScore());
						//把这个积分卡的积分变为0并且状态变为1
						sCard.setScore(Double.parseDouble("0"));
						sCard.setIsDelete("1");
						scoreCardService.updateScoreCard(sCard);
						return "3";   //成功
					} catch (DataException e) {
						return "2";   //充值出错，充值失败
					}
				}else{
					return "1"; //未找到用户
				}
			}else{
				return "0";   //
			}
		}
		
		/**
		 * 赠送积分，查询跳转mgr页面
		 */
		@RequestMapping("/presented.htm")
		public String presented(HttpServletRequest request,HttpServletResponse response){
			//得到当前登录人的一些必要信息
			User onlineUser = (User) request.getSession().getAttribute(Constants.LOGIN_USER);
			//request.setAttribute("onlineUser", onlineUser);    //存入当前登录人对象
			try {
				//加载啊必要数据
				List<ScoreGiftRecords> sGiftRecords = this.scoreGiftRecordService.findSGRs(onlineUser.getId());
				if(sGiftRecords.size() > 0){
					request.setAttribute("sGiftRecords", sGiftRecords);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			return "member/gift/scoreGiftRecordsMgr";
		}
		
		
		
		/**
		 * 删除积分赠送记录
		 */
		@RequestMapping("/deleteSGRs.htm")
		@ResponseBody
		public String deleteSGRs(HttpServletRequest request,HttpServletResponse response){
			String id = request.getParameter("id");
			ScoreGiftRecords sGiftRecords = this.scoreGiftRecordService.findSGRsById(Long.parseLong(id));
			if(sGiftRecords != null){
				try {
					this.scoreGiftRecordService.deleteSGRs(sGiftRecords);
				} catch (Exception e) {
					return "1";   //失败
				}
			}
			
			return "0";
		}
		
		/**
		 * 赠送积分，赠送跳转init页面
		 */
		@RequestMapping("/gift-init.htm")
		public String presentedURLs(HttpServletRequest request,HttpServletResponse response){
			User onlineUser = (User) request.getSession().getAttribute(Constants.LOGIN_USER);
			request.setAttribute("onlineUser", onlineUser);    //存入当前登录人对象
			return "member/gift/scoreGiftRecords-init";
		}
		
		/**
		 * 赠送积分，添加赠送记录
		 */
		@RequestMapping("/createSGRs.htm")
		@ResponseBody
		public String createSGRs(HttpServletRequest request,HttpServletResponse response){
			//接受参数
			String userid = request.getParameter("userId");
			String card = request.getParameter("card");
			String score = request.getParameter("score");
			String remark = request.getParameter("remark");
			if(!StringUtils.isEmpty(remark))remark = EscapeUtils.unescape(remark);
			
			String paramName = request.getParameter("paramName");
			if(!StringUtils.isEmpty(paramName))paramName = EscapeUtils.unescape(paramName);
			User onlineUser = this.userService.findUserById(Long.parseLong(userid));   //送积分者
			if(onlineUser != null){   //首先确保赠送者的存在
				//通过卡号找到接受积分人
				User toUser = this.userService.findUserByCondition(paramName, card).get(0);
				if(toUser != null){
					//将接受人的积分增加指定的额度并减少赠送人的相应额度
					toUser.getCard().setScore(toUser.getCard().getScore()+Double.parseDouble(score));
					try {
						this.userService.updateUser(toUser);    //执行赠送积分
						onlineUser.getCard().setScore(onlineUser.getCard().getScore()-Double.parseDouble(score));   //把赠送者的积分相应减少
						this.userService.updateUser(onlineUser);  //处理赠送者赠送积分后的积分额
						//将此次赠送写入表
						ScoreGiftRecords sGRs = new ScoreGiftRecords();
						sGRs.setFromId(onlineUser.getId());       //添加赠送人信息
						sGRs.setFromName(onlineUser.getUsername());  
						sGRs.setToId(toUser.getId());             //添加接受人信息
						sGRs.setToName(toUser.getUsername());
						sGRs.setScore(Double.parseDouble(score)); //添加积分额
						sGRs.setGiftTime(new Date());             //添加时间
						sGRs.setRemark(remark);                   
						
						try {
							this.scoreGiftRecordService.createOrder(sGRs);
						} catch (Exception e) {
							return "3";  //记录失败
						}
					} catch (DataException e) {
						return "1";   //赠送失败
					}       
				}else{
					return "0";    //执行者不存在
				}
			}
			return "2";    //成功
			
		}
		
		
		/**
		 * 我的订单模块
		 * 我的订单mgr页面跳转
		 */
		@RequestMapping("/myOrderURL.htm")
		public String myOrderURL(HttpServletRequest request,HttpServletResponse response){
			User onlineUser = (User) request.getSession().getAttribute(Constants.LOGIN_USER);
			request.setAttribute("onlineUser", onlineUser);    //存入当前登录人对象
			String pageNum = request.getParameter("pageNo");
			/*
			//加载订单状态数据字典
			List<Dictionary> orderStatus = DictUtils.getDictListByKind(DictUtils.ORDER_TYPE);
			request.setAttribute("orderStatus", orderStatus);*/
			
			if(StringUtils.isEmpty(pageNum))pageNum = Constants.FIRST_PAGE_NUM;
			
			//加载信息,检索用户全部订单
			Pagination pagination = this.orderService.findOrderPagination(onlineUser.getId(),15, Integer.parseInt(pageNum));
			request.setAttribute("pagination", pagination);
			return "member/order/myOrderMgr";
		}
		
		/**
		 * 确认订单，已经收到货
		 * @param request
		 * @param response
		 * @return
		 */
		@RequestMapping("/confirmProduct.htm")
		@ResponseBody
		public String confirmProduct(HttpServletRequest request,HttpServletResponse response){
			//接受订单id
			String orderId = request.getParameter("orderId");
			//查找订单
			Order order = this.orderService.findOrderById(Long.parseLong(orderId));
			if(order!=null){
				try {
					order.setStatus(4);
					this.orderService.updateOrder(order);
				} catch (Exception e) {
					return "1";
				}
			}else{
				return "2";  //未找到订单
			}
			return "0";
		}
		
		/**
		 * 我的订单模块
		 * 删除订单
		 * 根据删除
		 */
		@RequestMapping("/deleteOrder.htm")
		@ResponseBody
		public String deleteOrder(HttpServletRequest request,HttpServletResponse response){
			String id = request.getParameter("orderId");   //接搜订单id
			Order order = this.orderService.findOrderById(Long.parseLong(id));
			if(order!=null){
				try {
					//查找到订单下的全部商品
					List<OrderDetail> oDetails = this.orderDetailService.findOrderDetailList(order.getId());
					if(oDetails.size()>0){
						for (OrderDetail orderDetail : oDetails) {   //迭代集合，循环删除
							this.orderDetailService.deleteOrderDetail(orderDetail);
						}
					}
					this.orderService.deleteOrder(order); //在清除完毕这个订单的商品后，在删除订单
				} catch (Exception e) {
					return "1";
				}
			}else{
				return "2";  //未找到订单
			}
			return "0";
		}
		
		/**
		 * returnProductURL.htm
		 * 申请退货rProductMgr页面跳转
		 */
		@RequestMapping("/returnProductURL.htm")
		public String returnProductURL(HttpServletRequest request,HttpServletResponse response){
			String orderNo = request.getParameter("orderNo");    //接受订单号
			Order order = this.orderService.findOrderByOrderNo(orderNo);
			if(order!=null){
				//找出这个订单的全部商品
				List<OrderDetail> orderDetails = this.orderDetailService.findOrderDetailList(order.getId());
				if(orderDetails.size()>0){
					request.setAttribute("orderDetails", orderDetails);
				}
				request.setAttribute("order", order);
			}
			return "member/rProduct/rProductMgr";
		}
		
		/**
		 * 申请退货rProduct-init页面跳转
		 * 不支持批量处理
		 * 批量删除返回一个名为orderDetaiList的集合
		 */
		@RequestMapping("/returnProductInit.htm")
		public String returnProductInit(HttpServletRequest request,HttpServletResponse response){
			String orderDetailId = request.getParameter("orderDetailId");
			
			OrderDetail orderDetail = this.orderDetailService.findOrderDetailById(Long.parseLong(orderDetailId));
			if(orderDetail!=null){
				//List<OrderDetail> orderDetaiList = new ArrayList<OrderDetail>();
				request.setAttribute("orderDetail", orderDetail);
				request.setAttribute("orderId", orderDetail.getOrder().getId());   //退货时，需要指明是属于哪个订单的货
				//request.setAttribute("orderDetaiList",orderDetaiList);   //只作为页面的一个判断，对于单个申请无实际意义
				
				//加载退货理由的数据字典到select   DictUtils
				List<Dictionary> returnReasons = DictUtils.getDictListByKind(DictUtils.RETURN_REASONS);
				request.setAttribute("returnReasons", returnReasons);
			}
			
			return "member/rProduct/rProduct-init";
		}
		
		/**
		 * returnProduct.htm
		 * 我的订单模块
		 * 申请退货提交
		 */
		@RequestMapping("/returnProduct.htm")
		@ResponseBody
		public String returnProduct(HttpServletRequest request,HttpServletResponse response){
			String orderNo = request.getParameter("orderNo");    //接受订单编号
			String reason = request.getParameter("reason");      //退货原因
			if(!StringUtils.isEmpty(reason))reason = EscapeUtils.unescape(reason);
			
			String explain = request.getParameter("explain");    //退货说明
			if(!StringUtils.isEmpty(explain))explain = EscapeUtils.unescape(explain);
			
			String imageUrl = request.getParameter("imageUrl");  //退货凭证图片路径
			if(!StringUtils.isEmpty(imageUrl))imageUrl = EscapeUtils.unescape(imageUrl);
			
			Order order = this.orderService.findOrderByOrderNo(orderNo);  //找到这个订单
			if(order!=null){
				ReturnRequest returnRequest = new ReturnRequest();
				returnRequest.setOrder(order);      	//存入订单对象
				returnRequest.setReason(reason);    	//存入原因
				returnRequest.setProof1(imageUrl);  	//存入图片路径
				returnRequest.setDescripion(explain);   //说明
				try {
					this.returnRequestService.createReturnRequest(returnRequest);
					//把订单状态修改为7
					order.setStatus(7);
					this.orderService.updateOrder(order);
				} catch (Exception e) {
					return "1";    //添加失败
				}
				
			}else{
				return "2";  //未找到订单
			}
			return "0"; //成功
		}
		
		/**
		 * cancelProduct.htm
		 * 我的订单模块
		 * 取消退货申请
		 */
		@RequestMapping("/cancelProduct.htm")
		@ResponseBody
		public String cancelProduct(HttpServletRequest request,HttpServletResponse response){
			String orderId = request.getParameter("orderId");    //接受订单编号
			Order order = this.orderService.findOrderById(Long.parseLong(orderId));
			if(order!=null){
				order.setStatus(4);
				try {
					this.orderService.updateOrder(order);
				} catch (Exception e) {
					return "1";    //失败
				}
			}else{
				return "2";   //未找到订单
			}
			return "0";  //成功
		}
		
		
		/**
		 * findOrderProduct.htm
		 * 订单管理
		 * 查看订单商品跳转
		 */
		@RequestMapping("/findOrderProduct.htm")
		public String findOrderProduct(HttpServletRequest request,HttpServletResponse response){
			String id = request.getParameter("orderId");
			Order order = this.orderService.findOrderById(Long.parseLong(id));
			if(id!=null && order!= null){
				//查询出这个订单全部商品信息
				List<OrderDetail> oList = this.orderDetailService.findOrderDetailList(order.getId());
				if(oList.size()>0){
					request.setAttribute("oList", oList);
					request.setAttribute("orderId", order.getOrderNo());
				}
			}
			return "member/order/orderProduct";
		}
		
		/**
		 * 消费记录 mgr页面跳转
		 */
		@RequestMapping("/myRecordURL.htm")
		public String myRecordURL(HttpServletRequest request,HttpServletResponse response){
			User onlineUser = (User) request.getSession().getAttribute(Constants.LOGIN_USER);
			request.setAttribute("onlineUser", onlineUser);                    //存入当前登录人对象
			//加载显示数据
			List<ConsumerInfo> cList = consumerInfoService.findConsumerInfos(onlineUser.getId());
			if(cList != null){
				request.setAttribute("clist", cList);
			}
			return "member/record/consumptionMgr";
		}
		
		/**
		 * 删除操作,说明：并不是把数据从数据库清除，消费记录作为用户消费的凭证，当用户删除后，把其显示状态修改为会员不可见，但是管理员可以在后台查看到
		 */
		@RequestMapping("/updateCIFisDelete.htm")
		@ResponseBody
		public String updateCIFisDelete(HttpServletRequest request,HttpServletResponse response){
			String id  = request.getParameter("id");
			
			ConsumerInfo csInfo = this.consumerInfoService.findConsumerInfoById(Long.parseLong(id));
			if(csInfo != null){
				csInfo.setIsDelete("1");
				try {
					this.consumerInfoService.updateRechargeInfo(csInfo);
				} catch (DataException e) {
					return "0";
				}
			}
			return "1";
		}
		
		
}
